define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'terminal/index' + location.search,
                    add_url: 'terminal/add',
                    edit_url: 'terminal/edit',
                    del_url: 'terminal/del',
                    multi_url: 'terminal/multi',
                    import_url: 'terminal/import',
                    table: 'terminal',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sn', title: __('Sn'), operate: 'LIKE'},
                        {field: 'active_time', title: __('Active_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'binding_time', title: __('Binding_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('change', "#c-type", function () {
                const val = $(this).val();
                if(val == 1){
                    $(".stock").show();
                }else if(val == 2){
                    $(".stock").hide();
                }
            });

            $(document).on('change', "#c-to_type", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#company").show();
                    $("#user").hide();
                }else if(val == 2){
                    $("#company").hide();
                    $("#user").show();
                }
            });

            $(document).on('change', "#c-data_type", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#file_url").show();
                    $("#sn").hide();
                }else if(val == 2){
                    $("#file_url").hide();
                    $("#sn").show();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
